package com.jlnk.stockdemo.viewmodel;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jlnk.stockdemo.bean.NewsRequest;
import com.jlnk.stockdemo.bean.NewsResponse;
import com.jlnk.stockdemo.callback.ResponseCallback;
import com.jlnk.stockdemo.utils.NetworkUtils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class NewsViewModel extends ViewModel {
    private static final String TAG = "NewsViewModel";
    private MutableLiveData<List<NewsResponse.Data.News>> newsLiveData;

    public NewsViewModel() {
        newsLiveData = new MutableLiveData<>();
    }

    public LiveData<List<NewsResponse.Data.News>> getNewsLiveData() {
        return newsLiveData;
    }

    public void fetchNews() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date startDate = calendar.getTime();

        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        Date endDate = calendar.getTime();

        NewsRequest.Params params = new NewsRequest.Params();
        params.setStart_date(dateFormat.format(startDate));
        params.setEnd_date(dateFormat.format(endDate));
        params.setSrc("sina");

        NewsRequest newsRequest = new NewsRequest();
        newsRequest.setParams(params);
        NetworkUtils.sendPostRequest(newsRequest, new ResponseCallback() {
            @Override
            public void onResponse(String response) {
                // 在这里处理响应数据
                Gson gson = new Gson();
                Type type = new TypeToken<NewsResponse>(){}.getType();
                NewsResponse newsResponse = gson.fromJson(response, type);
                if (newsResponse != null) {
                    // 在这里处理解析后的数据
                    List<NewsResponse.Data.News> newsList = newsResponse.getData().getNewsList();
                    newsLiveData.postValue(newsList);
                }
            }

            @Override
            public void onPermissionError(String msg) {
                Log.d(TAG, "onPermissionError: " + msg);
            }

            @Override
            public void onInternalError(String msg) {
                Log.d(TAG, "onInternalError: " + msg);
            }

            @Override
            public void onFailure(IOException e) {
                Log.d(TAG, "onFailure: " + e.getMessage());
            }
        });
    }
}
